#include <graphics.h>  
#include <conio.h>
#include <stdio.h>
int main()
{
	float width,height,gravity;
	float ball_x,ball_y,ball_vy,radius; 
	float rect_left_x,rect_top_y,rect_width,rect_height,rect_vx; 
	int score = 0;

	width = 600;  
	height = 400; 
	gravity = 0.2;  
	initgraph(width, height);

	radius = 20; 
	ball_x = width/4;
	ball_y = height-radius;  
	ball_vy = 0;  

	rect_height = 100; 
	rect_width = 20;
	rect_left_x = width*3/4; 
	rect_top_y = height - rect_height;
	rect_vx = -3; 

	TCHAR s[20];
		_stprintf(s, _T(" Game start！")); 
		settextstyle(40, 0, _T("宋体")); 
		outtextxy(300, 200, s);

	Sleep(1000);
	
	while(1)
	{
		while(1) 
		{		
			if (kbhit()) 
			{
				char input = _getch(); 
				if (input==' ') 
			{
				ball_vy = -5; 
			}
		}
		
		ball_vy = ball_vy + gravity; 
		ball_y = ball_y + ball_vy; 
		if (ball_y >= height-radius) 
		{
			ball_vy = 0;  
			ball_y = height-radius; 
		}
	
		rect_left_x = rect_left_x + rect_vx;
		if (rect_left_x <= 0) 
		{
			rect_left_x = width; 
			score = score + 1; 
			rect_height = rand() % int(height/4) + height/4; 
			rect_vx = rand()/float(RAND_MAX) *3 - 7;
		}
		
		if ((rect_left_x <= ball_x + radius) 
			&& (rect_left_x + rect_width >= ball_x - radius) 
			&& (height - rect_height <= ball_y + radius) )
		{
			TCHAR s2[20];
		_stprintf(s2, _T("Ouch! :(")); 
		settextstyle(40, 0, _T("宋体")); 
		outtextxy(300, 200, s2);
			Sleep(50);
			score=0;
			cleardevice();
		}

		if ((rect_left_x <= ball_x + radius) 
			&& (rect_left_x + rect_width >= ball_x - radius) 
			&& (height-rect_height-130>= ball_y - radius) )
		{
			TCHAR s3[20];
		_stprintf(s3, _T("Ouch! :(")); 
		settextstyle(40, 0, _T("宋体")); 
		outtextxy(300, 200, s3);
			Sleep(50);
			score=0;
			cleardevice();
		}

		if (ball_y >= height-radius)
		{
			score=0;
			break;
			 
		}
		cleardevice();  
		fillcircle(ball_x, ball_y, radius);  
		
		fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width,height);
		fillrectangle(rect_left_x, 0, rect_left_x + rect_width,height-rect_height-130);
		TCHAR s[20];
		_stprintf(s, _T("%d"),  score); 
		settextstyle(40, 0, _T("宋体")); 
		outtextxy(50, 30, s); 
		Sleep(10); 
	}

	TCHAR s1[20];
		_stprintf(s1, _T("press blank to restart")); 
		settextstyle(20, 0, _T("宋体")); 
		outtextxy(170, 250, s1);
}
	closegraph(); 
	return 0;
}